<?php

namespace App\Http\Controller\Test;

use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Http\Server\Annotation\Mapping\RequestMethod;
use Swoft\Http\Message\Request;

/**
 * Class FastdfsController
 *
 * @Controller(prefix="Fastdfs")
 * @package App\Http\Controller\Test
 */
class FastdfsController extends BaseController {

    /**
     * 上传文件
     * @RequestMapping("upload",method={RequestMethod::POST})
     */
    public function upload(Request $request){
        return $request->post('image');

        //1.上传到FastDfs
        $info = $request->file('image');
        $fileName = $info->getClientFilename();
        $fileNameArr= explode(".",$fileName);
        $ext =array_pop($fileNameArr);
        $client = new \Eelly\FastDFS\Client(config('fastdfs'));
        $path = $client->uploadFile($info->getFile(),$ext);
        return $path;

        //2.上传到本地
        $info = $request->file('image');
        $fileName = $info->getClientFilename();
        $fileNameArr= explode(".",$fileName);
        $fixpre =array_pop($fileNameArr);
        $filePath  = "uploads/".date("Y",time())."-".date("m",time())."-".date("d",time())."/".md5(md5((string)time())).".".$fixpre;
        $info->moveTo("public/".$filePath);
        return "public/".$filePath;
    }

    /**
     * 删除文件
     * @RequestMapping(route="delete",method={RequestMethod::GET})
     */
    public function delete(Request $request){
        $filePath   = $request->input('filePath');
        $client = new \Eelly\FastDFS\Client(config('fastdfs'));
        return $client->deleteFile($filePath);
    }



    /**
     * 查看文件
     * @RequestMapping(route="view",method={RequestMethod::GET})
     */
    public function view(){
        return 'http://image.nhb.com/group1/M00/00/00/rBkAH2G5XZuAIIVBAAA20jWYN50191.png';
    }
}